<template>
  <div>
    <p>姓名<input type="text" v-model="form.name"></p>
    <p>年龄<input type="text" v-model="form.age"></p>
    <button @click="add">添加</button>

    <h1>展示此老师下的所以学生</h1>
    <ul>
      <li v-for="i in stulist">{{ i.name }} — {{ i.age }}
        <button @click="fun(i.id)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "student",
  data() {
    return {
      stulist: [],
      form: {'teacher': localStorage.getItem('tid')},
      tid: localStorage.getItem('tid'),
    }
  },

  methods: {
    add() {
      this.axios.post('stu/', this.form).then(res => {
        alert('ok')
        this.getstulist()
      })
    },
    getstulist() {
      this.axios.get('stu/?tid=' + this.tid).then(res => {
        this.stulist = res.data.list
      })
    },
    fun(id) {
      this.axios.put('stu/?id=' + id).then(res => {
        this.getstulist()
      })
    }
  },

  mounted() {
    this.getstulist()
    let tid = localStorage.getItem('tid')
    if (!tid) {
      alert('请登录后在操作')
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>

</style>
